package com.airline.connection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.airline.DAO.CustomerDAO;

public class DAOManager {
	
	private Map daoImplMap = new HashMap();
	public DAOManager() {

		daoImplMap.put("com.airline.DAO.EmployeeDAO", "com.airline.DAOImpl.EmployeeDAOImpl");
		daoImplMap.put("com.airline.DAO.CustomerDAO", "com.airline.DAOImpl.CustomerDAOImpl");
	}
	
	public DAO getDao(Class type) throws ClassNotFoundException{
		
		String name  = (String) daoImplMap.get(type.getName());
		Class impl = Class.forName(name);
		Constructor ctor = null;
		try {
			ctor = impl.getDeclaredConstructor(DAOManager.class);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    ctor.setAccessible(true);
	    DAO dao = null;
		try {
			 dao = (DAO)ctor.newInstance(this);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
		return dao;
		
	}
	
	
	public static void main(String[] args) {
	
		DAOManager mgr = new DAOManager();
		
		try {
			mgr.getDao(CustomerDAO.class);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
